/*
This software is distributed under the Simplified BSD License:

Copyright (c) 2008, Chris Venter <chris.venter@gmail.com>
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, 
    	this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, 
    	this list of conditions and the following disclaimer in the documentation 
    	and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
DAMAGE.
*/
#ifndef GENERIK_GTK_GLEXT_H
#define GENERIK_GTK_GLEXT_H

/** \file glext.h
This file declares a wrapper for gtkglext.
*/

#include <gtk/gtkgl.h>

#include "widget.h"
//#include "eventhandling.h"
#include "../opengl/context.h"
#include "../tracing.h"

#define GENERIK_GTK_GLINIT gtk_gl_init (&argc, &argv)

namespace generik
{

	namespace gtk
	{
	
		struct glext
		: widget
		//, dispatcher
		, realizer
		, opengl::context
		{
			typedef glext THIS;
			
			GdkGLConfig* _config;
			
			//int _w, _h;
			
			glext(GdkGLConfig* config_in, /*int w_in, int h_in,*/ int attribs = GDK_GL_RGBA_TYPE)
			: _config(config_in)
			//, _w(w_in)
			//, _h(h_in)
			{
				GtkWidget* opengldisplay = gtk_drawing_area_new();
				realizer::connect(opengldisplay);
				gtk_widget_set_name(opengldisplay, "opengldisplay");
				//gtk_widget_set_size_request(opengldisplay, _w, _h);
				//gtk_widget_set_default_size(opengldisplay, _w, _h);
				gtk_widget_set_gl_capability(opengldisplay, _config, NULL, TRUE, attribs);
				set_widget(opengldisplay);
			}
			
			/*
			void setsize(int w, int h)
			{
				_w = w;
				_h = h;
			}
			*/
			
			inline GdkGLContext* get_context()
			{
				return gtk_widget_get_gl_context(get_widget());
			}
			inline GdkGLDrawable* get_drawable()
			{
				return gtk_widget_get_gl_drawable(get_widget());
			}
			
			inline GdkGLDrawable* get_current()
			{
				return gdk_gl_drawable_get_current();
			}
			
			bool isdoublebuffered()
			{
				return gdk_gl_drawable_is_double_buffered(get_drawable());
			}
			
			/** Error handling?
			*/
			void begin()
			{
				//GENERIK_INLINE("GDK GL drawable begin: "<<(void*)this);
				gdk_gl_drawable_gl_begin(get_drawable(), get_context());
			}
			void end()
			{
				gdk_gl_drawable_gl_end(get_drawable());
				//GENERIK_INLINE("GDK GL drawable end:   "<<(void*)this);
			}
			
			void swapbuffers()
			{
				gdk_gl_drawable_swap_buffers(get_drawable());
			}
	
			void flush()
			{
				if (isdoublebuffered())
					swapbuffers();
				else
					glFlush();
			}
			
		};
		
	}
	
}

#endif









